package fr.generali.accueilclient.gwt.client.controller;

import com.google.web.bindery.event.shared.EventBus;
import com.google.web.bindery.event.shared.ResettableEventBus;

public abstract class AbstractController implements Controller {

    protected ResettableEventBus eventBus;

    private boolean started = false;

    private boolean stopped = false;

    public AbstractController() {
    }

    @Override
    public void startup(EventBus eventbus) {
        if (this.started) {
            throw new IllegalStateException("Controller already started");
        }
        this.started = true;
        this.eventBus = new ResettableEventBus(eventbus);
        bind();

    }

    @Override
    public void shutdown() {
        if (isStarted() && !isStopped()) {
            this.eventBus.removeHandlers();
            unbind();
        }
        this.stopped = true;

    }

    protected boolean isStopped() {
        return this.stopped;
    }

    protected abstract void bind();

    protected void unbind() {

    }

    public boolean isStarted() {
        return started;
    }
}
